package com.tang.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.reggie.common.R;
import com.tang.reggie.dto.DishDto;
import com.tang.reggie.dto.SetmealDto;
import com.tang.reggie.pojo.Setmeal;
import com.tang.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 添加
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//allEntries = true 清空setmealCache下所有缓存数据
    public R save(@RequestBody SetmealDto setmealDto){
        log.info("套餐模块：套餐添加功能--setmealDto: {}",setmealDto);
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("套餐添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        log.info("套餐模块：套餐分页查询功能");
        Page<SetmealDto> pageInfo=setmealService.select(page,pageSize,name);
        return R.success(pageInfo);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        log.info("套餐模块：套餐查询功能");
        SetmealDto setmealDto= setmealService.selectById(id);
        return R.success(setmealDto);
    }

    /**
     * 套餐修改功能
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//allEntries = true 清空setmealCache下所有缓存数据
    public R update(@RequestBody SetmealDto setmealDto){
        log.info("套餐模块：套餐添加功能--setmealDto: {}",setmealDto);
        setmealService.updateWithSetMealDto(setmealDto);
        return R.success("套餐修改成功");
    }

    /**
     * 套餐删除功能
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//allEntries = true 清空setmealCache下所有缓存数据
    public R delete(@RequestParam("ids") List<Long> ids){
        log.info("套餐模块：套餐删除功能");
        setmealService.delete(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 套餐状态修改
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)//allEntries = true 清空setmealCache下所有缓存数据
    public R updateByStatus(@PathVariable int status,@RequestParam("ids") List<Long> ids){
        log.info("套餐模块：套餐状态修改功能");
        setmealService.updateByStatus(status,ids);
        return R.success("状态修改成功");
    }

    /**
     * 展示所有套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R list(Setmeal setmeal){
        log.info("套餐模块：套餐展示");
        List<Setmeal> setmealList=setmealService.findByCategory(setmeal);
        return R.success(setmealList);
    }


    /**
     * 前台点击套餐，菜品展示
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R dish(@PathVariable Long id){
        log.info("套餐模块：前台点击套餐，菜品展示");
        List<DishDto> dishDtoList=setmealService.listDishs(id);
        return R.success(dishDtoList);
    }


}
